from IPython.display import HTML
HTML('''<button type="button" class="btn btn-outline-danger" onclick="codeToggle();">Toggle Code</button>''')
The data set contains information about three species of IRIS flowers namely:
Four features are collected from each sample, sepal-length, sepal-width, petal-length and petal-width in centi-meters.
# Common imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
iris_df = pd.read_csv("./data/IRIS.csv.gz", compression="gzip")
iris_df.sample(5)
| Unnamed: 0 | sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|---|
| 148 | 148 | 6.2 | 3.4 | 5.4 | 2.3 | Iris-virginica |
| 118 | 118 | 7.7 | 2.6 | 6.9 | 2.3 | Iris-virginica |
| 28 | 28 | 5.2 | 3.4 | 1.4 | 0.2 | Iris-setosa |
| 79 | 79 | 5.7 | 2.6 | 3.5 | 1.0 | Iris-versicolor |
| 135 | 135 | 7.7 | 3.0 | 6.1 | 2.3 | Iris-virginica |
fig = px.scatter_3d(iris_df, x='sepal_length', y='sepal_width', z='petal_width',
color='species', template="plotly_dark")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
covid_df = pd.read_csv("./data/owid-covid-data.csv.gz", compression="gzip")
covid_df.sample(5)
| Unnamed: 0 | iso_code | continent | location | date | total_cases | new_cases | new_cases_smoothed | total_deaths | new_deaths | ... | female_smokers | male_smokers | handwashing_facilities | hospital_beds_per_thousand | life_expectancy | human_development_index | excess_mortality_cumulative_absolute | excess_mortality_cumulative | excess_mortality | excess_mortality_cumulative_per_million | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 88604 | 88604 | OWID_LIC | NaN | Low income | 2020-04-18 | 4207.0 | 277.0 | 211.429 | 164.0 | 10.0 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 113927 | 113927 | OWID_OCE | NaN | Oceania | 2020-08-14 | 25132.0 | 327.0 | 367.857 | 405.0 | 4.0 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 56866 | 56866 | GEO | Asia | Georgia | 2020-08-13 | 1283.0 | 5.0 | 11.000 | 17.0 | 0.0 | ... | 5.3 | 55.5 | NaN | 2.6 | 73.77 | 0.812 | NaN | NaN | NaN | NaN |
| 150248 | 150248 | TGO | Africa | Togo | 2021-11-20 | 26199.0 | 0.0 | 7.286 | 243.0 | 0.0 | ... | 0.9 | 14.2 | 10.475 | 0.7 | 61.04 | 0.515 | NaN | NaN | NaN | NaN |
| 43528 | 43528 | ECU | South America | Ecuador | 2020-06-16 | 47943.0 | 621.0 | 575.143 | 3970.0 | 41.0 | ... | 2.0 | 12.3 | 80.635 | 1.5 | 77.01 | 0.759 | NaN | NaN | NaN | NaN |
5 rows × 68 columns
required_columns = ["iso_code", "location", "continent", "date", "new_cases_smoothed", "total_cases"]
covid_df = covid_df.dropna(subset = required_columns)
covid_df = covid_df.sort_values("date")
covid_df[['iso_code', 'location']].sample(8)
| iso_code | location | |
|---|---|---|
| 40387 | COD | Democratic Republic of Congo |
| 148642 | THA | Thailand |
| 73189 | IRQ | Iraq |
| 109618 | NER | Niger |
| 3082 | AND | Andorra |
| 25062 | BDI | Burundi |
| 117543 | PAN | Panama |
| 126394 | LCA | Saint Lucia |
covid_day_df = covid_df[covid_df.date == "2021-12-23"]
covid_day_df.sample(5)
| Unnamed: 0 | iso_code | continent | location | date | total_cases | new_cases | new_cases_smoothed | total_deaths | new_deaths | ... | female_smokers | male_smokers | handwashing_facilities | hospital_beds_per_thousand | life_expectancy | human_development_index | excess_mortality_cumulative_absolute | excess_mortality_cumulative | excess_mortality | excess_mortality_cumulative_per_million | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 126973 | 126973 | LCA | North America | Saint Lucia | 2021-12-23 | 13225.0 | 16.0 | 14.000 | 295.0 | 0.0 | ... | NaN | NaN | 87.202 | 1.30 | 76.20 | 0.759 | NaN | NaN | NaN | NaN |
| 21208 | 21208 | BWA | Africa | Botswana | 2021-12-23 | 208994.0 | 4293.0 | 1304.286 | 2427.0 | 2.0 | ... | 5.7 | 34.4 | NaN | 1.80 | 69.59 | 0.735 | NaN | NaN | NaN | NaN |
| 117553 | 117553 | PAN | North America | Panama | 2021-12-23 | 485512.0 | 719.0 | 526.857 | 7406.0 | 1.0 | ... | 2.4 | 9.9 | NaN | 2.30 | 78.51 | 0.815 | NaN | NaN | NaN | NaN |
| 84158 | 84158 | LVA | Europe | Latvia | 2021-12-23 | 270158.0 | 954.0 | 741.857 | 4489.0 | 12.0 | ... | 25.6 | 51.0 | NaN | 5.57 | 75.29 | 0.866 | NaN | NaN | NaN | NaN |
| 93696 | 93696 | MYS | Asia | Malaysia | 2021-12-23 | 2731713.0 | 3510.0 | 3473.000 | 31265.0 | 44.0 | ... | 1.0 | 42.4 | NaN | 1.90 | 76.16 | 0.810 | NaN | NaN | NaN | NaN |
5 rows × 68 columns
fig = px.scatter_geo(covid_day_df, locations="iso_code", color="continent",
hover_name="location", size="new_cases_smoothed",
projection="natural earth", template="plotly_dark")
fig.show()
fig = px.scatter_geo(covid_df, locations="iso_code", color="continent",
hover_name="location", size="total_cases",
projection="natural earth", animation_frame="date", template="plotly_dark")
fig.show()
import torchvision
import os
import matplotlib.pyplot as plt
from matplotlib import rc
from matplotlib.animation import FuncAnimation
from matplotlib import animation
rc('animation', html='jshtml')
frn = 10 # Number of frames to process in the animation
fps = 0.5 # Frames per second
mywriter = animation.PillowWriter(fps=fps)
mnist_dataset = torchvision.datasets.MNIST(root = "data/mnist", train = True, download = True, transform=torchvision.transforms.ToTensor())
fig, ax = plt.subplots(figsize = (10, 10))
def change_plot(frame_idx):
ax.cla()
image_tensor = mnist_dataset[frame_idx][0]
image_tensor_gray = image_tensor[0]
image_tensor_gray = image_tensor_gray * 255
ax.matshow(image_tensor_gray, cmap = "gray")
for i in range(image_tensor_gray.shape[0]):
for j in range(image_tensor_gray.shape[1]):
ax.text(i, j, str(int(image_tensor_gray[j][i].item())), va = "center", ha = "center", color = "blue", fontsize = "small")
ax.axis("off")
plt.tight_layout()
anim = FuncAnimation(fig, change_plot, frn, interval=1000 / fps)
plt.close()
anim
<Figure size 432x288 with 0 Axes>
mywriter = animation.PillowWriter(fps=fps)
if not os.path.exists("./assets/gif"):
os.makedir("./assets/gif")
anim.save('./assets/gif/mnist.gif',writer=mywriter)
<Figure size 432x288 with 0 Axes>
Credits: The below Hugging Face Space Demo was created by Prof. Nipun Batra for ES 654 2022 course. We have only embedded it in our notebook to demonstrate utility to allow user to run a model or interact with sliders, etc. in the notebook View original at: https://huggingface.co/spaces/Nipun/RidgeDemo
from IPython.display import HTML
HTML("""
<iframe src="https://hf.space/streamlit/Nipun/RidgeDemo/+" data-src="https://hf.space/streamlit/Nipun/RidgeDemo/+" data-sdk="streamlit" title="Streamlit app" height="800px" width="100%"></iframe>
""")